//
//  DYJCollectionViewController.m
//  Piece
//
//  Created by dllo on 16/6/15.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "DYJCollectionViewController.h"
#import "DYJDataBaseTool.h"
#import "DYJCollectionReadModel.h"
#import "DYJCollectionWebViewController.h"

@interface DYJCollectionViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *collectionTableView;
@property (nonatomic, strong) NSMutableArray *collectionDataArray;
@property (nonatomic, strong) UIImageView *noCollectionImageView;
@end

@implementation DYJCollectionViewController
- (void)dealloc
{
    _collectionTableView.delegate = nil;
    _collectionTableView.dataSource = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.76 green:0.79 blue:0.83 alpha:1.00];
    [self createTitle:@"文章"];
    [self createTableView];
    [self getDataArrayFromDataBase];
}
#pragma mark -- 创建tableView
- (void)createTableView
{
    _collectionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) style:UITableViewStylePlain];
    _collectionTableView.delegate = self;
    _collectionTableView.dataSource = self;
    _collectionTableView.separatorStyle = 0;
    [self.view addSubview:_collectionTableView];
    
    _noCollectionImageView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 20, 360, 240)];
    _noCollectionImageView.image = [UIImage imageNamed:@"collection"];
    [_collectionTableView addSubview:_noCollectionImageView];
    //注册cell
    [_collectionTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"collectionCell"];
}
#pragma mark -- 从数据库获取数组
- (void)getDataArrayFromDataBase
{
    _collectionDataArray = [NSMutableArray array];
    DYJDataBaseTool *dataBaseTool = [DYJDataBaseTool shareDYJDataBaseTool];
    [_collectionDataArray addObjectsFromArray:[dataBaseTool selectAll]];
    if (_collectionDataArray.count == 0) {
        _noCollectionImageView.hidden = NO;
    }
    else
    {
        _noCollectionImageView.hidden = YES;
    }
    [_collectionTableView reloadData];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _collectionDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectionCell"];
    DYJCollectionReadModel *collectionModel = [_collectionDataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = collectionModel.title;
    if ([collectionModel.type isEqualToString:@"short"]) {
        cell.imageView.image = [UIImage imageNamed:@"read_essay"];
    }
    else if ( [collectionModel.type isEqualToString:@"serial"])
    {
        cell.imageView.image = [UIImage imageNamed:@"read_serial"];
    }
    else
    {
        cell.imageView.image = [UIImage imageNamed:@"read_question"];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYJCollectionReadModel *collectionModel = [_collectionDataArray objectAtIndex:indexPath.row];
    DYJCollectionWebViewController *cwvc = [[DYJCollectionWebViewController alloc] init];
    cwvc.model = collectionModel;
    [self.navigationController pushViewController:cwvc animated:YES];
}
//------使tableView处于可编辑状态------//
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.collectionTableView setEditing:editing animated:editing];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
//完成编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYJCollectionReadModel *collectionModel = [_collectionDataArray objectAtIndex:indexPath.row];
    DYJDataBaseTool *dataBaseTool = [DYJDataBaseTool shareDYJDataBaseTool];
    [dataBaseTool delegateDataWithTitle:collectionModel.title];
    [_collectionDataArray removeObject:collectionModel];
    if (_collectionDataArray.count == 0) {
        _noCollectionImageView.hidden = NO;
    }
    else
    {
        _noCollectionImageView.hidden = YES;
    }

    [_collectionTableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
